package com.yangmf.auto_message_handle.service.serviceimpl;

import com.yangmf.auto_message_handle.constant.Event_Type;
import com.yangmf.auto_message_handle.pojo.dto.MesgDTO;
import com.yangmf.auto_message_handle.pojo.entity.Files;
import com.yangmf.auto_message_handle.pojo.entity.File;
import com.yangmf.auto_message_handle.pojo.entity.Mesg;
import com.yangmf.auto_message_handle.pojo.entity.StorageInfo;
import com.yangmf.auto_message_handle.pojo.vo.FileDetailVO;
import com.yangmf.auto_message_handle.pojo.vo.FileTypeVO;
import com.yangmf.auto_message_handle.mapper.DataMapper;
import com.yangmf.auto_message_handle.mapper.EventMapper;
import com.yangmf.auto_message_handle.pojo.vo.MesgVO;
import com.yangmf.auto_message_handle.service.DataService;
import com.yangmf.auto_message_handle.utils.AliOssUtil;
import com.yangmf.auto_message_handle.utils.ContentHighlightUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DataServiceImpl implements DataService {

    @Autowired
    private DataMapper dataMapper;
    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private ContentHighlightUtil contentHighlightUtil;
    @Override
    @Transactional
   public FileTypeVO getFilebyType(){



        StorageInfo storageInfo = aliOssUtil.getStorageInfo();
        log.info("存储信息: {}", storageInfo);

       List<Mesg> Messages = dataMapper.getmessagebyType();

        Files matcj_files = new Files(Event_Type.COMPETITION,new ArrayList<>());

        Files vol_files = new Files(Event_Type.VOLUNTEER,new ArrayList<>());

        Files ass_files = new Files(Event_Type.ASSIGNMENT,new ArrayList<>());

        Files notice_files = new Files(Event_Type.NOTICE,new ArrayList<>());

        Files other_files = new Files(Event_Type.OTHER,new ArrayList<>());

        for (Mesg message : Messages) {
            Integer file_type= message.getEventType();

            String s = parseName(message.getContent());

            switch (file_type){
                case Event_Type.COMPETITION:
                    matcj_files.getFiles().add(new File(s,message.getContent(),message.getCreateTime()));
                    break;
                case Event_Type.VOLUNTEER:
                    vol_files.getFiles().add(new File(s,message.getContent(),message.getCreateTime()));
                    break;
                case Event_Type.ASSIGNMENT:
                    ass_files.getFiles().add(new File(s,message.getContent(),message.getCreateTime()));
                case Event_Type.NOTICE:
                    notice_files.getFiles().add(new File(s,message.getContent(),message.getCreateTime()));
                    break;
                default:
                    other_files.getFiles().add(new File(s,message.getContent(),message.getCreateTime()));
                    break;

            }

        }
        return FileTypeVO.builder()
                .FolderSize(5)
                .TotalFileSize(storageInfo.getFormattedSize())
                .TotalFileCount(storageInfo.getFileCount())
                .fileList(List.of(matcj_files,vol_files,ass_files,notice_files,other_files))
                .build();

    }


    private String parseName(String content){
        String name = content.substring(content.lastIndexOf("/")+1);
        return name;
    }
    @Override
    @Transactional
    public FileDetailVO getDetailFileByType(Long type)
    {
        List<Mesg> messages = dataMapper.getFilebyEventType(type);

        List<File> collect = messages.stream().map(mesg -> {
            File file = new File(parseName(mesg.getContent()), mesg.getContent(), mesg.getCreateTime());
            return file;


        }).collect(Collectors.toList());

        return new FileDetailVO(type, collect);


    };
    @Override
    public  void mergeMessageToEvent(Long eventId,  List messageIds){

        dataMapper. updateEventIdById(messageIds,eventId);
    };

    @Override
    public  List<MesgVO> search(MesgDTO mesgDTO){
        log.info("检索内容{}",mesgDTO);
        Mesg mesg1 =new Mesg();
        BeanUtils.copyProperties(mesgDTO,mesg1);


        log.info("检索内容{}",mesg1);
        List<Mesg> mesgs = dataMapper.listofMessage(mesg1);

        List<MesgVO> collect = mesgs.stream().map(mesg -> MesgVO.builder().id(mesg.getId()).displayContent(contentHighlightUtil.highlightContent(mesg.getContent(), mesgDTO.getContent(), 5, 5)).eventId(mesg.getEventId()).build()).collect(Collectors.toList());

        return collect;
    };

    @Override
    public   void deleteMessage(List<Long> messageIds)
    {
        List<Mesg> mesgs = dataMapper.selectFilesByMessageId(messageIds);
        for (Mesg mesg : mesgs)
            aliOssUtil.deleteFile(mesg.getContent());
        dataMapper.batchdeleteMessagesById(messageIds);
    };

}
